﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MJM.SEDIC.EN;
using MJM.SEDIC.AD;

namespace MJM.SEDIC.CN
{
    public static class SubMenuCN
    {
        public static int Insertar(SubMenuEN obj)
        {
            obj.Estado = 1;
            return SubMenuAD.Insertar(obj);
        }

        public static List<SubMenuEN> Listar(int IdMenu)
        {
            SubMenuEN obj = new SubMenuEN();
            obj.Menu = IdMenu;
            return SubMenuAD.Listar(obj);
        }

        public static SubMenuEN Obtener(int idMenu,int IdSubMenu)
        { 
            List<SubMenuEN> lst = Listar(idMenu);
            for (int i = 0; i < lst.Count; i++)
            {
                if (IdSubMenu == lst[i].SubMenu)
                {
                    return lst[i];
                }
            }
            return null;
        }

        public static int Actualizar(SubMenuEN objSubMenuEN)
        {
            List<SubMenuEN> lst = Listar(objSubMenuEN.Menu);
            SubMenuEN origen = null, dest = null;
            for (int i = 0; i < lst.Count; i++)
            {
                if (lst[i].Orden == objSubMenuEN.Orden && lst[i].SubMenu != objSubMenuEN.SubMenu)
                    origen = lst[i];
                if (lst[i].SubMenu == objSubMenuEN.SubMenu)
                    dest = lst[i];
            }
            if (origen != null && dest != null)
            {
                origen.Orden = dest.Orden;
                SubMenuAD.Actualizar(origen);              
            }
            objSubMenuEN.Icono = dest.Icono;
            objSubMenuEN.Archivo = dest.Archivo;
            int result = SubMenuAD.Actualizar(objSubMenuEN);
            return result;
        }
    }
}
